package com.goldgov.config;

import com.goldgov.authentication.MockLoginAuthenticationProvider;
import com.goldgov.kduck.security.configuration.HttpSecurityConfigurer;
import com.goldgov.oauth2filter.PreRequestFilter;
import com.kcloud.ms.authentication.baseaccount.service.AccountCredentialService;
import com.kcloud.ms.authentication.baseaccount.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.stereotype.Component;

/**
 * 扩展securityConfig类，用于增加全局过滤器
 */
@Component
public class SecurityExt implements HttpSecurityConfigurer {
    @Autowired
    PreRequestFilter preRequestFilter;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private AccountService accountService;
    @Autowired
    private AccountCredentialService accountCredentialService;
    @Override
    public void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        http
                .addFilterBefore(preRequestFilter, UsernamePasswordAuthenticationFilter.class)
                .authenticationProvider(new MockLoginAuthenticationProvider(passwordEncoder,accountService,accountCredentialService))
        ;
        // @formatter:on
    }

    @Override
    public void configure(WebSecurity web) throws Exception {

    }
}
